#include "SWidget.h"
#include "Swindow.h"

SwidgetData::SwidgetData()
{
    x=0;
    y=0;
    w=200;
    h=25;
    isFocus=false;
    isVisible=true;
    bkColor=curColor={255,0,0,0};
    textColor={0,0,0,0};


}

Swidget::Swidget(Swidget* parent):m_data(new SwidgetData)
{
    Swindow::getSObjectList().push_back(this);
}

Swidget::~Swidget()
{
    delete m_data;
}

bool Swidget::event(SDL_Event* msg)
{
    switch (msg->type)
    {
    case SDL_MOUSEMOTION:
        mouseMoveEvent(msg);
        break;
    case SDL_MOUSEBUTTONDOWN:
        mousePressEvent(msg);
        break;
    case SDL_MOUSEBUTTONUP:
        mouseReleaseEvent(msg);
        break;
    case SDL_KEYDOWN:
        keyDownEvent(msg);
        break;
    case SDL_TEXTINPUT:
        inputEvent(msg);
        break;
    case SDL_TEXTEDITING:
        editEvent(msg);
        break;
    }


    if (m_data->isVisible)
    {
        paintEvent(msg);
    }
    if (m_data->isFocus)
    {

        m_data->curColor = {0,255,0,0};
    }else
    {
        m_data->curColor = {255,0,0,0};
    }
    return Sobj::event(msg);
}

void Swidget::settextcolor(const SDL_Color& color)
{
    m_data->textColor=color;
}

void Swidget::setbkcolor(const SDL_Color& color)
{
    m_data->bkColor = color;
}

void Swidget::setpos(int x, int y)
{
    m_data->x = x;
    m_data->y = y;

}

void Swidget::setpos(int x, int y, int w, int h)
{
    m_data->x = x;
    m_data->y = y;
    m_data->w = w;
    m_data->h = h;
}

void Swidget::setvisible(bool visible)
{
    m_data->isVisible=visible;
}

void Swidget::setfocus(bool focus)
{
    m_data->isFocus=focus;
}

bool Swidget::contains(int x, int y)
{
    int Lx = m_data->x;
    int Ly  = m_data->y;

    int rx = Lx + m_data->w;
    int ry = Ly + m_data->h;

    return (x>=Lx && x<=rx && y >= Ly && y<=ry);
}

void Swidget::paintEvent(SDL_Event* msg)
{
    SDL_SetRenderDrawColor(Swindow::render,m_data->curColor.r,m_data->curColor.g,m_data->curColor.b,m_data->curColor.a);
    SDL_Rect rect = {m_data->x,m_data->y,m_data->w,m_data->h};
    SDL_RenderFillRect(Swindow::render,&rect);

}

void Swidget::mousePressEvent(SDL_Event* msg)
{
}

void Swidget::mouseReleaseEvent(SDL_Event* msg)
{
}

void Swidget::mouseMoveEvent(SDL_Event* msg)
{
}

void Swidget::inputEvent(SDL_Event* msg)
{
}

void Swidget::editEvent(SDL_Event* msg)
{
}

void Swidget::keyDownEvent(SDL_Event* msg)
{
}

bool Swidget::getvisible()
{
    return m_data->isVisible;
}
